package Graphic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import jbook.util.Input;
import core.Administrator;

public class Program {
	private static Administrator a;
	private static boolean show = false;

	public static void main(String[] args) throws IOException {
		char c;
		try {
			System.out
					.println("Do you want to start a new game (a), or load (c) last one?");
			c = Input.readChar();
			if (c == 'a' || c == 'c') {
				if (c == 'a')
					/* It's a new game, so just position everything randomly */
					a = new Administrator();
				else {
					try {
						/* Load the game from RogueSave.txt */
						BufferedReader in = new BufferedReader(new FileReader(
								"RogueSave.txt"));

						String line;
						int num = 0;
						int entrance = 0;
						int exit = 0;
						int longSide = 0;
						int shortSide = 0;
						int playerPos = 0;
						int hp = 0;
						int maxHp = 0;
						int time = 0;
						Vector<Integer> monsters = new Vector<Integer>();
						Vector<Integer> walls = new Vector<Integer>();
						Vector<Integer> potions = new Vector<Integer>();
						while ((line = in.readLine()) != null) {
							if (line.contains("Wall")) {
								String[] parts = line.split("\"");
								walls.add(Integer.parseInt(parts[1]));
							} else if (line.contains("Room")) {
								String[] parts = line.split("\"");
								longSide = Integer.parseInt(parts[1]);
								shortSide = Integer.parseInt(parts[3]);
							} else if (line.contains("Entrance")) {
								String[] parts = line.split("\"");
								entrance = Integer.parseInt(parts[1]);
							} else if (line.contains("Exit")) {
								String[] parts = line.split("\"");
								exit = Integer.parseInt(parts[1]);
							} else if (line.contains("Player")) {
								String[] parts = line.split("\"");
								hp = Integer.parseInt(parts[1]);
								maxHp = Integer.parseInt(parts[3]);
								playerPos = Integer.parseInt(parts[5]);
							} else if (line.contains("Monster")) {
								String[] parts = line.split("\"");
								monsters.add(Integer.parseInt(parts[5]));
							} else if (line.contains("Potion")) {
								String[] parts = line.split("\"");
								potions.add(Integer.parseInt(parts[3]));
							} else if (line.contains("Time")) {
								String[] parts = line.split("\"");
								time = Integer.parseInt(parts[1]);
							}
							num++;
						}
						in.close();
						if (num == 0)
							throw new SaveFileException();
						/*
						 * If the game is saved right at the start, since the
						 * matrix can only contain one element and the player is
						 * at the entrance, the entrance position is the same as
						 * the player, even thought it is not indicated in the
						 * save file (it's not in the matrix)
						 */
						if (time == 0)
							entrance = playerPos;
						a = new Administrator(entrance, exit, walls, longSide,
								shortSide, playerPos, hp, maxHp, time,
								monsters, potions);
					} catch (SaveFileException e) {
						System.out.println("Empty save file");
						System.exit(1);
					} catch (FileNotFoundException e) {
						System.out.println("No save file found");
						System.exit(2);
					}
				}
				System.out
						.println("Do you want to play an easy (e) or hard (h) game?");
				c = Input.readChar();
				if (c == 'e' || c == 'h') {
					if (c == 'e')
						show = true;
				} else
					throw new WrongInputException();
				while (!(a.checkFinish())) {
					do {
						if (show)
							a.getCurrentRoom().printRoom();
						else
							a.getCurrentRoom().printRoomHard();
						System.out.println(a.getPlayer());
						System.out
								.printf("\n Where do you want to move? (or input s to save)");
						c = Input.readChar();
						if (c == 's') {
							PrintWriter save = new PrintWriter(new File(
									"RogueSave.txt"));
							for (int i = 0; i < (a.getCurrentRoom()
									.getLongSide() * a.getCurrentRoom()
									.getShortSide()); i++)
								if (a.getCurrentRoom().getElement(
										i / a.getCurrentRoom().getLongSide(),
										i % a.getCurrentRoom().getLongSide()) != null)
									save.println(a.getCurrentRoom().getElement(
											i
													/ a.getCurrentRoom()
															.getLongSide(),
											i
													% a.getCurrentRoom()
															.getLongSide()));
							save.println("Time=\"" + a.getTime() + "\"");
							save.println(a.getCurrentRoom());
							save.close();
							/* After saving the status, just exit */
							System.exit(0);
						}
					} while (!(a.playerMove(c)));
					if (a.clearMobs())
						System.out
								.println("\n\n-------------->Player killed a monster!<--------------\n");
					a.moveMobs();
					if (a.clearMobs())
						System.out
								.println("\n\n-------------->Something died!<--------------\n");
					a.time();
				}
				a.getCurrentRoom().printRoom();
				if (a.getPlayer().getHp() == 0)
					System.out.println("Game over, you're dead!");
				else
					System.out.println("Congratulations, you WON!!!");
			} else
				throw new WrongInputException();
		} catch (WrongInputException e) {
			System.out.println("You inserted the wrong input");
		} catch (StringIndexOutOfBoundsException e) {
			System.out
					.println("**************You didn't insert anything**************");
		}
	}
}
